/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ToxicBakery.pagetransformer.page;

import com.ToxicBakery.pagetransformer.ResourceTable;
import com.ToxicBakery.pagetransformer.bean.TransformerItem;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import java.util.ArrayList;

/**
 * 选择动画条目适配器
 *
 * @since 2021-04-12
 */
public class ListProvider extends BaseItemProvider {
    private ArrayList<TransformerItem> transformers = new ArrayList<>();

    /**
     * 创建动画条目适配器
     *
     * @param transformers 动画集合
     */
    public ListProvider(ArrayList<TransformerItem> transformers) {
        this.transformers.addAll(transformers);
    }

    @Override
    public int getCount() {
        return transformers.size();
    }

    @Override
    public Object getItem(int position) {
        return transformers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Holder holder;
        if (component == null) {
            holder = new Holder();
            component = LayoutScatter.getInstance(componentContainer.getContext()).parse(
                ResourceTable.Layout_item_layout, null, false);

            holder.text = (Text) component.findComponentById(ResourceTable.Id_tv_item);
            component.setTag(holder);
        } else {
            holder = (Holder) component.getTag();
        }
        holder.text.setText(transformers.get(i).getName());
        return component;
    }

    /**
     * Holder
     *
     * @since 2021-04-12
     */
    static class Holder {
        Text text;
    }
}
